package com.example.young.peopleaccommodationms;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import Adapters.ShowInfoAdapter;
import DB.OrdersBean;
import DB.FileStoreRegInfo;
import DB.ShowInfoItem;

public class ShowInfo extends Activity implements Cloneable{

    public ListView listView;
    public List<Map<String, Object>> data;
    public List<OrdersBean> ordersList_static = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_info);

        listView = (ListView) findViewById(R.id.listview_showinfo);
        try {
            data = getData();
//            Log.i("tag",String.valueOf(data.size()));

        } catch (Exception e) {
            e.printStackTrace();
        }
        final ShowInfoAdapter showInfoAdapter = new ShowInfoAdapter(this,data);
        listView.setAdapter(showInfoAdapter);



        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(ShowInfo.this, OrdersInfoEnsure.class);

                ShowInfoItem itemInfo = showInfoAdapter.getItem(position);
                String order_user_id = itemInfo.getId();
                OrdersBean ordersBeanRes = getOrderByItemID(order_user_id);
                intent.putExtra("orders", ordersBeanRes);

                startActivity(intent);

            }
        });

    }

    private List<Map<String, Object>> getData() throws  Exception {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        List<OrdersBean> ordersList = new  ArrayList < OrdersBean >();
        Map<String, Object> map;
        if (ordersList == null || ordersList.size() == 0)
        {
            ordersList = FileStoreRegInfo.queryAllOrders();
            for (OrdersBean ite: ordersList) {
                ordersList_static.add(ite);

            }

        }
//        Log.i("tag",String.valueOf(ordersList.size()));
        Iterator<OrdersBean> iterator = ordersList.iterator();
        while (iterator.hasNext()) {
            OrdersBean orders = iterator.next();
            map = new HashMap<String, Object>();
            map.put("img", R.mipmap.ic_launcher);
            map.put("name", orders.getName());
            map.put("id", orders.getId());
//            if (list.contains(map))
//                continue;
//            else
                list.add(map);
        }
        ordersList.clear();
        return list;
    }

    //根据身份证号得到对应订单
    public OrdersBean getOrderByItemID(String id) {
        Iterator<OrdersBean> iterator = ordersList_static.iterator();
        OrdersBean ordersBean = null;
        while ((iterator.hasNext())) {
            ordersBean = iterator.next();
            if (ordersBean.getId().equals(id)) {
                return ordersBean;
            }
        }
        return ordersBean;
    }


}
